﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.dataaccess;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using InHD.commands;
using System.Windows;
using System.Data;
using InHD.utilities;
using InHD.Properties;

namespace InHD.viewmodels
{
    class AccPayableViewModel: WorkspaceViewModel
    {

        #region Constructor
        public AccPayableViewModel(CompanyInfo companyInfo)
        {
            _companyInfo = companyInfo;
            this._supplierRepository = new SupplierRepository();
            this._supplier = Supplier.CreateSupplier();
            LoadSupplierList();
            _transactionRepository = new TransactionRepository();

            this.PageContent = new AccPayableListViewModel(this._supplierRowCollection);
            this.PageContent.RequestClose += this.HandleCloseCurrentPage;
        }

        #endregion

        #region Value Property
        CompanyInfo _companyInfo;
        Supplier _supplier;
        SupplierRepository _supplierRepository;
        private List<Supplier> _supplierRowCollection = new List<Supplier>();

        Transaction _transaction;
        TransactionRepository _transactionRepository;
        List<Transaction> _transactionRowCollection = new List<Transaction>();

        private WorkspaceViewModel _pageContent;
        #endregion

        #region Presentation Property

        public WorkspaceViewModel PageContent
        {
            get
            {
                return _pageContent;
            }
            set
            {
                _pageContent = value;
            }
        }

        #endregion

        #region Normal Method
        private void LoadSupplierList()
        {
            this._supplierRepository.LoadAllSuppliers();
            this._supplierRowCollection = this._supplierRepository.GetSuppliers();
        }

        private void LoadTransactionWthSupplier()
        {
            _transactionRowCollection = null;
            _transactionRowCollection = this._transactionRepository.GetTransactionByCustomer(this._supplier.SupplierUNK);
        }

        private void HandleCloseCurrentPage(object source, EventArgs e)
        {
            this.PageContent.RequestClose -= this.HandleCloseCurrentPage;
            if (this.PageContent is AccPayableListViewModel)
            {
                string commandExcuted = ((AccPayableListViewModel)PageContent).commandExcute;
                if (string.IsNullOrEmpty(commandExcuted))
                {
                    OnRequestClose();
                    return;
                }

                if (commandExcuted.Equals("ViewTransactionList"))
                {
                    this._supplier = ((AccPayableListViewModel)PageContent).SelectedSupplier;

                    //// Load all transaction with selected supplier
                    LoadTransactionWthSupplier();

                    this.PageContent = new AccPayableList2ViewModel(_transactionRowCollection);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
            }
            else
            {
                if (this.PageContent is AccPayableList2ViewModel)
                {
                    string commandExcuted = ((AccPayableList2ViewModel)PageContent).commandExcute;
                    if (commandExcuted.Equals("CloseTransactionList"))
                    {
                        this.PageContent = new AccPayableListViewModel(_supplierRowCollection);
                        this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                        OnPropertyChanged("PageContent");
                    }

                    if (commandExcuted.Equals("ViewTransactionDetail"))
                    {
                        _transaction = ((AccPayableList2ViewModel)PageContent).SelectedTransaction;
                        this.PageContent = new AccPayableFormViewModel(_transaction, _companyInfo);
                        this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                        OnPropertyChanged("PageContent");
                    }
                }
                else
                {
                    if (this.PageContent is AccPayableFormViewModel)
                    {
                        string commandExcuted = ((AccPayableFormViewModel)PageContent).commandExcute;
                        if (commandExcuted.Equals("CloseTransactionDetail"))
                        {
                            LoadTransactionWthSupplier();
                            this.PageContent = new AccPayableList2ViewModel(_transactionRowCollection);
                            this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                            OnPropertyChanged("PageContent");
                        }

                        if (commandExcuted.Equals("GetAccpayable"))
                        {
                            LoadTransactionWthSupplier();
                            this.PageContent = new AccPayableList2ViewModel(_transactionRowCollection);
                            this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                            OnPropertyChanged("PageContent");
                        }
                    }
                }
            }
        }
        #endregion

        public override string DisplayName
        {
            get
            {
                try
                {
                    return Resources.Title_Payable;
                }
                catch (Exception ex)
                {
                    return string.Empty;
                }
            }

        }
    }
}
